#!/bin/bash
DIR="/tmp"

cd $DIR
curl -fsSL https://gitee.com/kirainmoe/static-files/raw/master/enhancer/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist > /tmp/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist
curl -fsSL https://gitee.com/kirainmoe/static-files/raw/master/enhancer/TongfangEnhancerDaemon > /tmp/TongfangEnhancerDaemon
curl -fsSL https://gitee.com/kirainmoe/static-files/raw/master/enhancer/fancli > /tmp/fancli

# remove old daemon and conflict daemon
echo "Installing TongfangEnhancerDaemon:"
echo "Removing old and conflict daemons..."

launchctl unload /Library/LaunchAgents/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist > /dev/null 2>&1
sudo rm /Library/LaunchAgents/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist > /dev/null 2>&1
sudo rm /usr/local/bin/TongfangEnhancerDaemon > /dev/null 2>&1

launchctl unload /Library/LaunchAgents/io.github.goshin.HotkeyDaemon.plist > /dev/null 2>&1
sudo rm /Library/LaunchAgents/io.github.goshin.HotkeyDaemon.plist > /dev/null 2>&1
sudo rm /usr/local/bin/HotkeyDaemon > /dev/null 2>&1

launchctl unload /Library/LaunchAgents/io.github.goshin.TongfangFnDaemon.plist > /dev/null 2>&1
sudo rm /Library/LaunchAgents/io.github.goshin.TongfangFnDaemon.plist > /dev/null 2>&1
sudo rm /usr/local/bin/TongfangFnDaemon > /dev/null 2>&1

# copy daemon
echo "Copy executable files..."
sudo mkdir -p /usr/local/bin/
sudo chmod -R 755 /usr/local/bin/
sudo cp $DIR/TongfangEnhancerDaemon /usr/local/bin/
sudo chmod 755 /usr/local/bin/TongfangEnhancerDaemon
sudo chown root:wheel /usr/local/bin/TongfangEnhancerDaemon

sudo cp $DIR/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist /Library/LaunchAgents
sudo chmod 644 /Library/LaunchAgents/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist
sudo chown root:wheel /Library/LaunchAgents/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist

sudo launchctl load /Library/LaunchAgents/io.github.goshin.and.com.kirainmoe.TongfangEnhancerDaemon.plist

nohup /usr/local/bin/TongfangEnhancerDaemon &

# copy fancli
echo "Installing fancli"
sudo cp $DIR/fancli /usr/local/bin/
sudo chmod 755 /usr/local/bin/fancli
sudo chown root:wheel /usr/local/bin/fancli

echo "Done."
